package org.template.dao;

import org.springframework.stereotype.Repository;
import org.template.dao.iter.RedisDelegator;
import org.template.dao.iter.RedisDelegatorNoResult;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

@Repository
public class TopicRedisDao extends BaseRedisDao {
	
	
	/**
	 * 用redis  实现一个简易的观察者模式 需要自己继承抽象类JedisPubSub
	 * @param edisPubSub
	 * @param isPattern
	 * @param channelName
	 */
	public void subscribeChannel(final JedisPubSub  edisPubSub,final boolean isPattern,final String ... channelName){
		
		runNoResult(new RedisDelegatorNoResult() {
			@Override
			public void execute(Jedis redis) {
				if (isPattern){
					redis.psubscribe(edisPubSub, channelName);
				}else{
					redis.subscribe(edisPubSub, channelName);
				}
			}
		});
	}
	
	public long publishChannelMsg(final String channelName,final String msg){
		
		return run(new RedisDelegator<Long>() {
			@Override
			public Long execute(Jedis redis) {
				return redis.publish(channelName, msg);
			}
		});
	
	}

}
